<template>
  <div class="addobject">
    <div class="outmain ba_f heighthv pad_20 bor_rad5">
      <div class="shuju_title mar_b20">
        <div class="shuju_title_text">
          <span>新增自提点</span>
        </div>
      </div>
      <div class="mar_t20 flex-bet flex-y-top">
        <el-form ref="form" :model="form" label-width="180px">
          <el-form-item label="自提点名称" :required="true" prop="name">
            <el-input v-model="form.name" placeholder="请输入自提点名称"></el-input>
          </el-form-item>
          <el-form-item label="联系人" :required="true" prop="linkMan">
            <el-input v-model="form.linkMan" placeholder="请输入联系人"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" :required="true" prop="tel">
            <el-input v-model="form.tel" placeholder="请输入联系电话"></el-input>
          </el-form-item>
          <el-form-item label="自提地址" :required="true">
            <el-input placeholder="请输入地址" v-model="form.address" style="width: 400px">
              <el-button slot="append" @click="searchMapClick">搜索</el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="经纬度" :required="true">
            <el-input v-model="form.lng" disabled style="width: 130px" class></el-input>
            <el-input v-model="form.lat" disabled style="width: 130px" class="mar_l20"></el-input>
          </el-form-item>
          <el-form-item label="地图定位">
            <select-map ref="mapCom" :mapLng="form.lng" :mapLat="form.lat" :mapAddress="form.address" @clickmap="getMapdata"></select-map>
          </el-form-item>
          <el-form-item label="状态">
            <el-tooltip :content="form.display==1?'显示':'隐藏'" placement="top">
              <el-switch v-model="form.display" active-value="1" inactive-value="2"></el-switch>
            </el-tooltip>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="mar_t20 ba_f pad_20 t_c">
      <el-button type="primary" @click="submitForm">立即创建</el-button>
    </div>
  </div>
</template>
<script>
import { getSaveSelf, postSaveSelf } from '@/api/plug';
import selectMap from '@/plugins/selectmap';

export default {
  components: { selectMap },
  created() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.edit();
    }
  },
  data() {
    return {
      form: {
        lng: 114.304692,
        lat: 30.59331,
        address: '湖北省武汉市',
        name: '',
        tel: '',
        linkMan: '',
        display: '1'
      }
    };
  },
  methods: {
    getMapdata(mapAddress, mapLat, mapLng) {
      this.form.address = mapAddress;
      this.form.lat = mapLat;
      this.form.lng = mapLng;
    },
    searchMapClick() {
      this.$refs.mapCom.searchKeyword();
    },
    async edit() {
      const { data } = await getSaveSelf({ id: this.id });
      this.form2 = this.form;
      this.form = Object.assign(this.form2, data);
      this.form.lat = +this.form2.lat;
      this.form.lng = +this.form2.lng;
    },
    getUeditor(content) {
      this.form.details = content;
    },
    async submitForm() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const { msg } = await postSaveSelf(this.form);
          this.$baseMessage(msg, 'success');
          this.$router.go(-1);
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss">
.addobject {
  .time_item .el-input {
    width: 180px;
  }
}
</style>
